package com.ftg.learn.day13;


import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
 * 学习一下io
 *
 * @author 布衣
 */
public class LearnIO {

    /**
     * 第一种  :  捕获       try catch finally
     * 第二种  ： 手工抛出    throw
     * 第三种  ： 声明式抛出  throws 用在方法声明后面
     * 第四种  :  try with resources
     *
     * @param pathName 地址
     * @return true|false
     */
    public boolean createFile(String pathName) {
        checkPathName(pathName);
        File f = new File(pathName);
        try {
            return f.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private void checkPathName(String pathName) {
        if (pathName == null || pathName.trim().isEmpty()) {
           KjxException.illageA();
        }
    }

    /**
     * 创建目录的
     *
     * @param pathName 地址
     * @return true|false
     */
    public boolean createDir(String pathName) {
        checkPathName(pathName);
        File f = new File(pathName);
        return f.mkdirs();
    }

    public boolean deleteDir(String pathName) {
        checkPathName(pathName);
        File f = new File(pathName);
        return !f.exists() || f.delete();
    }

    /**
     * 自己写的
     *
     * @param pathName 地址
     * @return true|false
     */
    public boolean createFileByPath(String pathName) {
        checkPathName(pathName);
        File f = new File(pathName);
        if (f.exists()) {
            return true;
        }


        if (!f.isFile()) {
            String dic = pathName.substring(0, pathName.lastIndexOf("\\"));
            File f1 = new File(dic);
            if (f1.isDirectory()) {
                return createFile(pathName);
            }
            createDir(dic);
            return createFile(pathName);
        }
        return createDir(pathName);
    }

    /**
     * 递归打印文件名
     *
     * @param fileName 文件名
     */
    public void printFileName(File fileName) {
        if (fileName.isFile()) {
            System.out.println(fileName.getName());
        } else {
            File[] files = fileName.listFiles();
            for (File file : files) {
                printFileName(file);
            }
        }
    }

    public void printMessToFile(String pathName,String mess) throws IOException {
        File f = new File(pathName);
        if(!f.exists()){
            f.createNewFile();
        }
        String s = "public class aa{\npublic static void main(String[] args){\nSystem.out.println(\""+mess+"\");\n}\n}";
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        //输出流对象
        try (
                FileOutputStream fos = new FileOutputStream(f);
        ) {
            fos.write(bytes);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws IOException {

        List<Integer> d = Arrays.asList(1,null);

//        字节流
        //fileoutputstream

        //字符流  FileReader FileWriter  无缓冲区的
//        带缓冲区的
//        Writer
        //读流  ：  有过多成input
        BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\lovex\\Desktop\\技术分享课.txt"));
        String str="";
        while (reader.ready()) {
            str += reader.readLine();
        }
        System.out.println(str);
        reader.close();

        BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\lovex\\Desktop\\技术分享课.txt"));
        writer.write(str);
        writer.newLine();
        writer.write("fsdf好");
        writer.close();

//        Reader reader = new FileReader("C:\\Users\\lovex\\Desktop\\技术分享课.txt");
//        File f = new File("C:\\Users\\lovex\\Desktop\\技术分享课1.txt");
//        f.createNewFile();
//        Writer writer = new FileWriter(f);
//        while (reader.ready()) {
//            writer.write(reader.read());
////            System.out.print((char) reader.read());
//        }
//        writer.close();
//        reader.close();

//        LearnIO l = new LearnIO();
//        FileInputStream fis = new FileInputStream("C:\\Users\\lovex\\Desktop\\aa.java");
//        String newFileName = "C:\\Users\\lovex\\Desktop\\aanew.java";
//        l.createFileByPath(newFileName);
//        FileOutputStream fos = new FileOutputStream(newFileName);
//        int n = 0;
//        while( (n = fis.read()) != -1){
//            fos.write(n);
//        }
//
//        fis.close();
//        fos.close();
    }

}
